﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Model;
using System.IO;
using System.Xml.Serialization;
using GameHelp;

namespace TdyxAutoBot
{
    public partial class ConfigForm : Form
    {
        Account account = null;
        GameConfig config = null;
        public ConfigForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 此方法已被隐藏，请使用 public void Show(GameConfig config);
        /// </summary>
        public new void Show()
        {
            throw new NotSupportedException();
        }

        public void Show(Account account)
        {
            this.account = account;
            config = Manager.GetConfig(account);
            this.cbxRadio30Easy.Checked = (config.Radio30Type == RadioType.Easy && config.AutoRadio30);
            this.cbxRadio30Hard.Checked = (config.Radio30Type == RadioType.Hard && config.AutoRadio30);
            this.cbxRadio50Easy.Checked = (config.Radio50Type == RadioType.Easy && config.AutoRadio50);
            this.cbxRadio50Hard.Checked = (config.Radio50Type == RadioType.Hard && config.AutoRadio50);
            this.cbxRadio70Easy.Checked = (config.Radio70Type == RadioType.Easy && config.AutoRadio70);
            this.cbxRadio70Hard.Checked = (config.Radio70Type == RadioType.Hard && config.AutoRadio70);
            this.cbxRadio80Hard.Checked = (config.Radio80Type == RadioType.Hard && config.AutoRadio80);
            this.cbxRadio80Easy.Checked = (config.Radio80Type == RadioType.Easy && config.AutoRadio80);

            this.cbx随行任务.Checked = config.AutoDialyQuest;
            this.rad自动挂机.Checked = config.AutoGuaji;
            this.radQuest30.Checked = config.RadioQuestType == RadioQuest.Radio30;
            this.radQuest50.Checked = config.RadioQuestType == RadioQuest.Radio50;
            this.radQuest70.Checked = config.RadioQuestType == RadioQuest.Radio70;
            this.Text = "自动游戏配置    角色：" + account.Character.Name;
            this.radClose.Checked = config.WindowState == ClientWindowState.Close;
            this.radHide.Checked = config.WindowState == ClientWindowState.Hide;
            this.radShow.Checked = config.WindowState == ClientWindowState.Show;
            this.cbxAutoLogin.Checked = config.AutoLogin;
            this.cbx经验任务.Checked = config.AutoExpQuest;

            this.comboBoxExpQuestColor.Items.Add(ExpTaskColor.白色);
            this.comboBoxExpQuestColor.Items.Add(ExpTaskColor.绿色);
            this.comboBoxExpQuestColor.Items.Add(ExpTaskColor.蓝色);
            this.comboBoxExpQuestColor.Items.Add(ExpTaskColor.紫色);
            this.comboBoxExpQuestColor.Items.Add(ExpTaskColor.橙色);
            this.cbx经验任务只做前10.Checked = config.OnlyTask10;

            foreach (object o in this.comboBoxExpQuestColor.Items)
            {
                if (((ExpTaskColor)o) == config.NeedExpTaskColor)
                {
                    this.comboBoxExpQuestColor.SelectedItem = o;
                }
            }

            base.ShowDialog();
        }

        //保存
        private void button1_Click(object sender, EventArgs e)
        {
            ReadConfigData();
            string message;
            if (config.Save(account))
            {
                message = account.ToString("_") + "的配置已保存.";
            }
            else
            {
                message = account.ToString("_") + "的配置保存失败.";
            }

            this.toolTip1.Show(message, this, this.btnSave.Location);
            //new TimeCloseWindow().Show(message);
        }

        //保存并复制到所有账户
        private void btnCopyAll_Click(object sender, EventArgs e)
        {
            ReadConfigData();
            string message;
            bool allSaved = true;
            if (config.Save(account))
            {
                message = account.ToString("_") + "的配置已保存.";
            }
            else
            {
                message = account.ToString("_") + "的配置保存失败.";
            }

            this.toolTip1.Show(message, this, this.btnSave.Location, 3000);

            #region 保存其他账户配置信息
            foreach (Account acc in Manager.AccountList)
            {
                GameConfig otherConfig = Manager.GetConfig(acc);
                if (otherConfig != null)
                {
                    #region 复制配置信息
                    otherConfig.AutoAttackMonster = config.AutoAttackMonster;
                    otherConfig.AutoDialyQuest = config.AutoDialyQuest;
                    otherConfig.AutoGuaji = config.AutoGuaji;
                    otherConfig.AutoLogin = config.AutoLogin;
                    otherConfig.AutoRadio30 = config.AutoRadio30;
                    otherConfig.AutoRadio50 = config.AutoRadio50;
                    otherConfig.AutoRadio70 = config.AutoRadio70;
                    otherConfig.AutoRadio80 = config.AutoRadio80;
                    otherConfig.AutoRadio90 = config.AutoRadio90;
                    otherConfig.MonsterID = config.MonsterID;
                    otherConfig.Radio30Type = config.Radio30Type;
                    otherConfig.Radio50Type = config.Radio50Type;
                    otherConfig.Radio70Type = config.Radio70Type;
                    otherConfig.Radio80Type = config.Radio80Type;
                    otherConfig.Radio90Type = config.Radio90Type;
                    otherConfig.RadioQuestType = config.RadioQuestType;
                    otherConfig.WindowState = config.WindowState;
                    otherConfig.OnlyTask10 = config.OnlyTask10;
                    otherConfig.NeedExpTaskColor = config.NeedExpTaskColor;
                    otherConfig.AutoExpQuest = config.AutoExpQuest;
                    #endregion
                    if (otherConfig.Save(acc))
                    {
                        message = acc.ToString("_") + "的配置已保存.";
                    }
                    else
                    {
                        allSaved = false;
                        message = acc.ToString("_") + "的配置保存失败.";
                    }
                    this.toolTip1.Show(message, this, this.btnSave.Location, 3000);
                }
            }
            #endregion

            if (allSaved)
            {
                message = "已成功复制配置信息到其他账户并保存.";
            }
            else
            {
                message = "已成功复制配置信息到其他账户并保存，部分账户没有保存成功。";
            }

            this.toolTip1.Show(message, this, this.btnCopyAll.Location, 3000);
        }

        private void ReadConfigData()
        {
            config.Radio30Type = cbxRadio30Easy.Checked ? RadioType.Easy : RadioType.Hard;
            config.Radio50Type = cbxRadio50Easy.Checked ? RadioType.Easy : RadioType.Hard;
            config.Radio70Type = cbxRadio70Easy.Checked ? RadioType.Easy : RadioType.Hard;
            config.AutoRadio30 = cbxRadio30Easy.Checked || cbxRadio30Hard.Checked;
            config.AutoRadio50 = cbxRadio50Easy.Checked || cbxRadio50Hard.Checked;
            config.AutoRadio70 = cbxRadio70Easy.Checked || cbxRadio70Hard.Checked;
            config.AutoGuaji = this.rad自动挂机.Checked;
            config.AutoDialyQuest = cbx随行任务.Checked;
            config.RadioQuestType = this.radQuest30.Checked ? RadioQuest.Radio30 : this.radQuest50.Checked ? RadioQuest.Radio50 : RadioQuest.Radio70;
            config.WindowState = this.radShow.Checked ? ClientWindowState.Show : this.radHide.Checked ? ClientWindowState.Hide : ClientWindowState.Close;
            config.AutoLogin = this.cbxAutoLogin.Checked;
            config.OnlyTask10 = this.cbx经验任务只做前10.Checked;
            config.NeedExpTaskColor = (ExpTaskColor)this.comboBoxExpQuestColor.SelectedItem;
            config.AutoExpQuest = this.cbx经验任务.Checked;
        }

        //修改自动副本的选中状态
        private void cbxRadio30Easy_CheckedChanged(object sender, EventArgs e)
        {
            string checkBoxName = "cbxRadio";
            CheckBox checkBox = (CheckBox)sender;
            if (checkBox.Text.IndexOf("30") > -1)
            {
                checkBoxName += "30";
            }
            else if (checkBox.Text.IndexOf("50") > -1)
            {
                checkBoxName += "50";
            }
            else if (checkBox.Text.IndexOf("70") > -1)
            {
                checkBoxName += "70";
            }
            else if (checkBox.Text.IndexOf("80") > -1)
            {
                checkBoxName += "80";
            }

            if (checkBox.Text.IndexOf("困难") > -1)
            {
                checkBoxName += "Easy";
            }
            else
            {
                checkBoxName += "Hard";
            }

            if (checkBox.Checked)
            {
                ((CheckBox)this.groupBox1.Controls.Find(checkBoxName, true)[0]).Checked = false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
